// Create the canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);

// Background image 加载背景图片
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function () {
	bgReady = true;  // 标志背景图片加载完成
};
bgImage.src = "images/background.png";

// Hero image 加载英雄图片
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function () {
	heroReady = true;  // 标志英雄图片加载完成
};
heroImage.src = "images/hero.png";

// Monster image 加载怪兽图片
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function () {
	monsterReady = true;	// 标志怪兽图片加载完成
};
monsterImage.src = "images/monster.png";

// Game objects
var hero = {
	speed: 256 // movement in pixels per second  英雄的属性
};
var monster = {};
var monstersCaught = 0;

// Handle keyboard controls
var keysDown = {};

// 键盘被按下
addEventListener("keydown", function (e) {
	keysDown[e.keyCode] = true;
}, false);

// 键盘被松开
addEventListener("keyup", function (e) {
	delete keysDown[e.keyCode];
}, false);

// 开始新一轮游戏
// Reset the game when the player catches a monster
var reset = function () {

	// 重新把英雄放到canvas中间
	hero.x = canvas.width / 2;
	hero.y = canvas.height / 2;

	// 随机放置怪兽
	// Throw the monster somewhere on the screen randomly
	monster.x = 32 + (Math.random() * (canvas.width - 64));
	monster.y = 32 + (Math.random() * (canvas.height - 64));
};

// Update game objects => 负责更新游戏的各个对象，会被规律地重复调用
var update = function (modifier) {

	// 检查用户当前按住的是哪一个方向键，然后将英雄往相应方向移动
	if (38 in keysDown) { // Player holding up
		hero.y -= hero.speed * modifier;
	}
	if (40 in keysDown) { // Player holding down
		hero.y += hero.speed * modifier;
	}
	if (37 in keysDown) { // Player holding left
		hero.x -= hero.speed * modifier;
	}
	if (39 in keysDown) { // Player holding right
		hero.x += hero.speed * modifier;
	}

	// Are they touching? => 判断怪物和英雄是否碰撞
	if (
		hero.x <= (monster.x + 32)
		&& monster.x <= (hero.x + 32)
		&& hero.y <= (monster.y + 32)
		&& monster.y <= (hero.y + 32)
	) {
		++monstersCaught;
		reset();
	}
};

// Draw everything => 把所有东西都画出来
var render = function () {
	
	// 画出背景图
	if (bgReady) {
		ctx.drawImage(bgImage, 0, 0);
	}

	// 画出英雄
	if (heroReady) {
		ctx.drawImage(heroImage, hero.x, hero.y);
	}

	// 画出怪兽
	if (monsterReady) {
		ctx.drawImage(monsterImage, monster.x, monster.y);
	}

	// Score
	ctx.fillStyle = "rgb(250, 250, 250)";
	ctx.font = "24px Helvetica";
	ctx.textAlign = "left";
	ctx.textBaseline = "top";
	// 绘制文字
	ctx.fillText("Goblins caught: " + monstersCaught, 32, 32);
};

// The main game loop => 循环结构
var main = function () {
	var now = Date.now();
	var delta = now - then;

	update(delta / 1000);
	render();

	then = now; 

	// Request to do this again ASAP
	requestAnimationFrame(main);  // 按帧对网页进行重绘
};

// Cross-browser support for requestAnimationFrame => 浏览器兼容性的解决
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;

// Let's play this game!
var then = Date.now();
reset();
main();
